package com.zdlog.wenjuan.controller.web;

import com.zdlog.wenjuan.VO.ResultBoolenVO;
import com.zdlog.wenjuan.dao.ProblemOptionRepository;
import com.zdlog.wenjuan.dataobject.ProblemDetail;
import com.zdlog.wenjuan.dataobject.ProblemOption;
import com.zdlog.wenjuan.dto.web.ProblemDetailDTO;
import com.zdlog.wenjuan.dto.web.ProblemOptionDTO;
import com.zdlog.wenjuan.enums.ProblemTypeEnum;
import com.zdlog.wenjuan.service.MainProblemService;
import com.zdlog.wenjuan.service.ProblemService;
import com.zdlog.wenjuan.util.KeyUtil;
import com.zdlog.wenjuan.util.ResultBoolenVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: wenjuanSer
 * @PACKAGE_NAME: com.zdlog.wenjuan.controller
 * @author: Kuaile
 * @create: 2018-02-06 16:10
 **/

@Controller
@RequestMapping("/problem")
@ResponseBody
@Slf4j
public class ProblemController {


    @Autowired
    private MainProblemService service;


    @Autowired
    private ProblemService problemService;


    @Autowired
    private ProblemOptionRepository optionRepository;

    @GetMapping("/addProblem")
    public ModelAndView addProblem(@RequestParam(value = "id") String formid,
                                   Map<String, Object> map) {
        map.put("mainProblem", service.list(formid));
        return new ModelAndView("problem/add");
    }

    @PostMapping("/toAdd")
    public ResultBoolenVO toAddProblem(HttpServletRequest request) {
        String detailId = KeyUtil.genUniqueKey();
        String optionVal = "";
        List<String> option = new ArrayList<>();
        List<ProblemOption> problemOptionList = new ArrayList<>();
        ProblemOption problemOption = null;

        String mainProblem = request.getParameter("mainProblem");
        String problemName = request.getParameter("problemName");
        String type = request.getParameter("type");
        String must = request.getParameter("must");

        try {
            for (int i = 1; i <= 10; i++) {
                optionVal = request.getParameter(i + "");
                if (!optionVal.equals("")) {
                    option.add(optionVal);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        ProblemDetail problemDetail = new ProblemDetail();
        problemDetail.setMainProblemId(mainProblem);
        problemDetail.setProblemType(Integer.parseInt(type));
        problemDetail.setProblemName(problemName);
        problemDetail.setIsMust(Integer.parseInt(must));
        problemDetail.setProblemDetailId(detailId);

        for (int i = 0; i < option.size(); i++) {
            problemOption = new ProblemOption();
            problemOption.setProblemId(detailId);
            problemOption.setSerialNumber(i + 1);
            problemOption.setProblemType(new Integer(type));
            problemOption.setOptionName(option.get(i));
            problemOptionList.add(problemOption);
        }
        if (problemService.addProblem(problemDetail, problemOptionList))
            return ResultBoolenVOUtil.success();
        return ResultBoolenVOUtil.error("失败");
    }


    @GetMapping("edit")
    public ModelAndView edit(@RequestParam(value = "id") String problemId,
                             @RequestParam(value = "formid") String formid,
                             Map<String, Object> map) {
        ProblemDetail problemDetail = problemService.findByProblemId(problemId);
        ProblemDetailDTO problemDetailDTO = null;
        if (problemDetail.getProblemType() == ProblemTypeEnum.DANXUAN.getCode() || problemDetail.getProblemType() == ProblemTypeEnum.DUOXUAN.getCode()) {
            problemDetailDTO = new ProblemDetailDTO();
            BeanUtils.copyProperties(problemDetail, problemDetailDTO);
            List<ProblemOption> problemOptionList = optionRepository.findByProblemIdOrderBySerialNumberAsc(problemId);
            ProblemOptionDTO problemOptionDTO = null;
            for (ProblemOption problemOption : problemOptionList) {
                problemOptionDTO = new ProblemOptionDTO();
                BeanUtils.copyProperties(problemOption, problemOptionDTO);
                problemDetailDTO.problemOptionDTOList.add(problemOptionDTO);
            }

        } else {
            problemDetailDTO = new ProblemDetailDTO();
            BeanUtils.copyProperties(problemDetail, problemDetailDTO);
        }
        map.put("mainProblem", service.list(formid));
        map.put("problem", problemDetailDTO);
        return new ModelAndView("problem/edit");

    }


    @PostMapping("/doDelete")
    public ResultBoolenVO doDelete(@RequestParam(value = "id") String problemId) {
        return problemService.delete(problemId);
    }


    @PostMapping("/doEdit")
    public ResultBoolenVO doEdit(HttpServletRequest request,@RequestParam(value = "id") String problemId) {
        String detailId = problemId;
        String optionVal = "";
        List<String> option = new ArrayList<>();
        List<ProblemOption> problemOptionList = new ArrayList<>();
        ProblemOption problemOption = null;

        String mainProblem = request.getParameter("mainProblem");
        String problemName = request.getParameter("problemName");
        String type = request.getParameter("type");
        String must = request.getParameter("must");

        try {
            for (int i = 1; i <= 10; i++) {
                optionVal = request.getParameter(i + "");
                if (!optionVal.equals("")) {
                    option.add(optionVal);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        ProblemDetail problemDetail = new ProblemDetail();
        problemDetail.setMainProblemId(mainProblem);
        problemDetail.setProblemType(Integer.parseInt(type));
        problemDetail.setProblemName(problemName);
        problemDetail.setIsMust(Integer.parseInt(must));
        problemDetail.setProblemDetailId(detailId);

        for (int i = 0; i < option.size(); i++) {
            problemOption = new ProblemOption();
            problemOption.setProblemId(detailId);
            problemOption.setSerialNumber(i + 1);
            problemOption.setProblemType(new Integer(type));
            problemOption.setOptionName(option.get(i));
            problemOptionList.add(problemOption);
        }
        if (problemService.updataProblem(problemDetail, problemOptionList))
            return ResultBoolenVOUtil.success();
        return ResultBoolenVOUtil.error("失败");
    }

}
